package com.shell.mapadapter;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private int [] img={R.drawable.bg1,R.drawable.bg2,R.drawable.bg3,R.drawable.bg4,R.drawable.bg5,R.drawable.bg6,R.drawable.bg7,R.drawable.bg8,R.drawable.bg9,R.drawable.bg10};
    private String[] names={"刘德华","阿悠悠","阿YueYue","罗翔","马云","张杰","尤雨溪","光头强","李白","武大郎"};
    private String[] occupations={"演员","歌手","歌手","刑法老师","企业老总","歌手","独立开源开发者","砍树","诗人","卖炊饼"};
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.list);
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,
                getDate(),
                R.layout.layout,
                new String[]{"img","name","occupation"},
                new int[]{R.id.iv_image,R.id.tv_name,R.id.tv_occupation});
        listView.setAdapter(simpleAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,"你选择的名字是："+names[position]+"\n职业是："+occupations[position],Toast.LENGTH_LONG).show();
            }
        });
    }
    public List<Map<String,Object>>getDate(){
        List<Map<String,Object>> list= new ArrayList<>();
        for (int i=0;i<names.length;i++){
            Map<String,Object>map=new HashMap<>();
            map.put("img",img[i]);
            map.put("name",names[i]);
            map.put("occupation",occupations[i]);
            list.add(map);
        }
        return list;
    }
}
